using System.Collections.Generic;
using Godot;

public partial class BattleManager : Node2D
{
    RandomNumberGenerator random;
    #region 子组件

    CanvasLayer UIRoot;
    ProgressBar progressBarLeft;
    ProgressBar progressBarRight;

    Label healthLeft;
    Label healthRight;
    Label maxHealthLeft;
    Label maxHealthRight;

    Label weaponNameLeft;
    Label weaponNameRight;
    Label hitRateLeft;
    Label hitRateRight;
    TextureRect robotIconLeft;
    TextureRect robotIconRight;
    Label robotNameLeft;
    Label robotNameRight;
    Label robotLevelLeft;
    Label robotLevelRight;

    Label attackLeft;
    Label attackRight;
    Label defenseLeft;
    Label defenseRight;
    Label speedLeft;
    Label speedRight;

    TextureRect avatarLeft;
    TextureRect avatarRight;
    Label pilotNameLeft;
    Label pilotNameRight;

    Sprite2D sprite2DLeft;
    Sprite2D sprite2DRight;

    BaseLevelManager levelManager;
    BaseMapManager mapManager;

    Label labelInfo;
    TextureRect actorAvatar;
    Label actorName;
    Label actorWords;

    CanvasLayer buttonRoot;
    Button buttonNext;
    #endregion

    Robot leftRobot;
    Robot rightRobot;

    Node weaponAnimates;

    List<BaseWeaponAnimate> weaponAnimateList = new();

    void GetChildComponents()
    {
        UIRoot = GetNode<CanvasLayer>("CanvasLayer");
        buttonRoot = GetNode<CanvasLayer>("ButtonRoot");
        progressBarLeft = GetNode<ProgressBar>("%ProgressBarLeft");
        progressBarRight = GetNode<ProgressBar>("%ProgressBarRight");
        healthLeft = GetNode<Label>("%HealthLeft");
        healthRight = GetNode<Label>("%HealthRight");
        maxHealthLeft = GetNode<Label>("%MaxHealthLeft");
        maxHealthRight = GetNode<Label>("%MaxHealthRight");
        weaponNameLeft = GetNode<Label>("%WeaponNameLeft");
        weaponNameRight = GetNode<Label>("%WeaponNameRight");
        hitRateLeft = GetNode<Label>("%HitRateLeft");
        hitRateRight = GetNode<Label>("%HitRateRight");
        robotIconLeft = GetNode<TextureRect>("%RobotIconLeft");
        robotIconRight = GetNode<TextureRect>("%RobotIconRight");
        robotNameLeft = GetNode<Label>("%RobotNameLeft");
        robotNameRight = GetNode<Label>("%RobotNameRight");
        robotLevelLeft = GetNode<Label>("%RobotLevelLeft");
        robotLevelRight = GetNode<Label>("%RobotLevelRight");
        attackLeft = GetNode<Label>("%AttackLeft");
        attackRight = GetNode<Label>("%AttackRight");
        defenseLeft = GetNode<Label>("%DefLeft");
        defenseRight = GetNode<Label>("%DefRight");
        speedLeft = GetNode<Label>("%SpeedLeft");
        speedRight = GetNode<Label>("%SpeedRight");
        avatarLeft = GetNode<TextureRect>("%AvatarLeft");
        avatarRight = GetNode<TextureRect>("%AvatarRight");
        pilotNameLeft = GetNode<Label>("%PilotNameLeft");
        pilotNameRight = GetNode<Label>("%PilotNameRight");
        sprite2DLeft = GetNode<Sprite2D>("%Sprite2DLeft");
        sprite2DRight = GetNode<Sprite2D>("%Sprite2DRight");

        weaponAnimates = GetNode<Node>("WeaponAnimates");

        labelInfo = GetNode<Label>("%LabelInfo");
        actorAvatar = GetNode<TextureRect>("%ActorAvatar");
        actorName = GetNode<Label>("%ActorName");
        actorWords = GetNode<Label>("%ActorWords");
        buttonNext = GetNode<Button>("%ButtonNext");
        buttonNext.Pressed += OnInputWatcherGuiInput;
    }

    public void UpdateScene(WeaponEntity weaponFromAttacker, WeaponEntity weaponFromTarget)
    {
        maxHealthLeft.Text = leftRobot.Entity.TotalHealth.ToString();
        maxHealthRight.Text = rightRobot.Entity.TotalHealth.ToString();
        UpdateHPBar(leftRobot);
        UpdateHPBar(rightRobot);
        if (weaponFromAttacker.OwnerRobot == leftRobot)
        {
            weaponNameLeft.Text = weaponFromAttacker?.WeaponName ?? "";
            weaponNameRight.Text = weaponFromTarget?.WeaponName ?? "";
        }
        else
        {
            weaponNameLeft.Text = weaponFromTarget?.WeaponName ?? "";
            weaponNameRight.Text = weaponFromAttacker?.WeaponName ?? "";
        }
        sprite2DLeft.Texture = GameUtil.GetRobotPortrait(leftRobot.Entity.TemplateID);
        sprite2DRight.Texture = GameUtil.GetRobotPortrait(rightRobot.Entity.TemplateID);
        robotIconLeft.Texture = GameUtil.GetRobotIcon(leftRobot.Entity.Icon, leftRobot.IsPlayer, leftRobot.IsAlly);
        robotIconRight.Texture = GameUtil.GetRobotIcon(rightRobot.Entity.Icon, rightRobot.IsPlayer, rightRobot.IsAlly);
        robotNameLeft.Text = leftRobot.Entity.RobotName;
        robotNameRight.Text = rightRobot.Entity.RobotName;
        robotLevelLeft.Text = leftRobot.Entity.Level.ToString();
        robotLevelRight.Text = rightRobot.Entity.Level.ToString();
        attackLeft.Text = leftRobot.Entity.TotalAttack.ToString();
        attackRight.Text = rightRobot.Entity.TotalAttack.ToString();
        defenseLeft.Text = leftRobot.Entity.TotalDefense.ToString();
        defenseRight.Text = rightRobot.Entity.TotalDefense.ToString();
        speedLeft.Text = leftRobot.Entity.TotalSpeed.ToString();
        speedRight.Text = rightRobot.Entity.TotalSpeed.ToString();
        pilotNameLeft.Text = leftRobot.Entity.Pilot.PilotName;
        pilotNameRight.Text = rightRobot.Entity.Pilot.PilotName;
        avatarLeft.Texture = GameUtil.GetPilotAvatar(leftRobot.Entity.Pilot.TemplateID);
        avatarRight.Texture = GameUtil.GetPilotAvatar(rightRobot.Entity.Pilot.TemplateID);
    }

    public void Init(BaseLevelManager level, BaseMapManager map)
    {
        levelManager = level;
        mapManager = map;
    }

    public override void _Ready()
    {
        base._Ready();
        GetChildComponents();
        random = new RandomNumberGenerator();
    }
}